angular.module('FlowCharge.service',['FlowCharge.unit']);


//数据传输服务
angular.module('FlowCharge.service').service('DtoService',['$http',function($http){
    this.vo = {};

    this.set = function(name,val){
      this.vo.name = val;
    }
    this.get = function(name){
      return this.vo.name||null;
    }
}]);

//获取城市数据
angular.module('FlowCharge.service').service('AreaService',['$http',function($http){

  this.getAreaJson = function(){ //获取城市数据
    var promise = $http({
        url:'js/js/city.json',
        method:'GET'
      }).then(function(data){
        return  data;

      });

      return promise;
  }

  this.buyCard = function(vo){
  var promise =   $http({"url":"native.php",
              "method": "post",
               "data":vo,
               "headers":{'Content-Type': 'application/x-www-form-urlencoded'},
               "transformRequest": function(obj) {
                  var str = [];
                  for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                  }
                  return str.join("&");
                }
             }).then(function(data){
               return  data;

             });

             return promise;
  }

  this.getShopMifiCardList = function(){
    var promise = $http({
        url:'js/js/city.json',
        method:'GET'
      }).then(function(data){
        return  data;

      });

      return promise;
  }

  }

])


angular.module('FlowCharge.service').service('HistorySerivce',['$cookie',function($cookie){ //其底层可以是cookie实现也可以是服务器保存
    this.addHistory = function(item){ //添加历史记录
          var history = $cookie.get('history');
          if (history && history.length > 0) {
              historyList = history.split('-');
              historyList.unshift(item);
              historyList = $.unique(historyList);
              history = historyList.join('-');
          } else {
              history = item;
          }
          console.log(history);
          $cookie.set('history', history, 'd30');

    }
    this.getHistory = function(){
      var history = $cookie.get('history');
      var historyList=[];
      if (history && history.length > 0) {
          historyList = history.split('-');
        }
        return historyList;
    }
    this.delHistory = function(){

    }

}])

angular.module('FlowCharge.service').service('PayMentService',['$http','$q','BASE_URL',function($http,$q,BASE_URL){
  this.wechat = function(data,uiCallback,sucessCallback){ //弹出二维码 扫码
       var deferred= $q.defer();
         $http({"url":BASE_URL+"protected/charge.php",
                   "method": "post",
                    "data":data,
                    "headers":{'Content-Type': 'application/x-www-form-urlencoded'},
                    "transformRequest": function(obj) {
                       var str = [];
                       for(var p in obj){
                         str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                       }
                       return str.join("&");
                     }
                  })
               .success(function(response){

                  if(!response.code_url){
                      deferred.reject(response);
                  }else{
                      deferred.resolve(response);
                  }


               })
               .error(function(reason){
                  deferred.reject(reason);
               });
        return deferred.promise;
  }
  this.queryOrder = function(out_trade_no){
     var deferred= $q.defer();
    $http.get('orderStatus.php?out_trade_no='+out_trade_no)
         .success(function(data){
              if(data.success){
                  deferred.resolve(data);
              }else{
                  deferred.reject(data);
              }

         })


  }



  this.alipay = function(data){ //未实现


  }

}]);
